#!/bin/bash
#Barry Kauler 2008.
#Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html). 2007 www.puppylinux.com
#a gui to provide choices for "events" -- things that occur at a systems level,
#usually hardware related.
#w007 mouse inactivity computer power-down.
#110505 support sudo for non-root user.
#131123 zigbert: gui (gtkdialog) improvements.

#set -x

[ "`whoami`" != "root" ] && exec sudo -A ${0} ${@} #110505

[ -z "$ROX_DESKTOP" ] && pidof ROX-Filer &>/dev/null && ROX_DESKTOP=yes

export TEXTDOMAIN=eventmanager
export OUTPUT_CHARSET=UTF-8

SHOWMODE=$1 #v405 optional to modify gui.

. /etc/rc.d/PUPSTATE
. /etc/DISTRO_SPECS
PUPPYVERSION="$DISTRO_VERSION"
KERNVER="`uname -r`"
. /etc/rc.d/MODULESCONFIG

[ -f /etc/eventmanager ] && . /etc/eventmanager
[ ! $RAMSAVEINTERVAL ] && RAMSAVEINTERVAL=30
[ "$ASKTOSAVE" != "true" -a "$ASKTOSAVE" != "false" ] && ASKTOSAVE="false"
[ "$HOTPLUGNOISY" != "true" -a "$HOTPLUGNOISY" != "false" ] && HOTPLUGNOISY="false"
[ "$HOTPLUGON" != "true" -a "$HOTPLUGON" != "false" ] && HOTPLUGON="true"
[ "$BACKENDON" != "true" -a "$BACKENDON" != "false" ] && BACKENDON="true"
[ ! $POWERTIMEOUT ] && POWERTIMEOUT=0 #w007 0=never.

#w007 as have a optional cutdown gui, make sure these are all preset...
NEWRAMSAVEINTERVAL=$RAMSAVEINTERVAL
NEWHOTPLUGNOISY="$HOTPLUGNOISY"
NEWHOTPLUGON="$HOTPLUGON"
NEWBACKENDON="$BACKENDON"
NEWPOWERTIMEOUT=$POWERTIMEOUT

if [ "$ROX_DESKTOP" ] ; then
	[ "$ICONDESK" != "true" -a "$ICONDESK" != "false" ] && ICONDESK="true"
	[ "$ICONPARTITIONS" != "true" -a "$ICONPARTITIONS" != "false" ] && ICONPARTITIONS="true"
	[ "$LABELPARTITIONS" != "true" -a "$LABELPARTITIONS" != "false" ] && LABELPARTITIONS="true" 
	[ "$FD0ICON" != "true" -a "$FD0ICON" != "false" ] && FD0ICON="true"
	case $ICON_PLACE_ORIENTATION in
		bottom|top|right|left) ok=1 ;;
		*) ICON_PLACE_ORIENTATION="bottom" ;;
	esac
	[ ! "$ICON_PLACE_EDGE_GAP" ] && ICON_PLACE_EDGE_GAP=80
	[ ! "$ICON_PLACE_START_GAP" ] && ICON_PLACE_START_GAP=32
	[ ! "$ICON_PLACE_SPACING" ] && ICON_PLACE_SPACING=64
	#-
	NEWICONDESK="$ICONDESK"
	NEWICONPARTITIONS="$ICONPARTITIONS"
	NEWLABELPARTITIONS="$LABELPARTITIONS"
	NEWFD0ICON="$FD0ICON"
	NEWICON_PLACE_ORIENTATION="$ICON_PLACE_ORIENTATION"
	NEWICON_PLACE_EDGE_GAP="$ICON_PLACE_EDGE_GAP"
	NEWICON_PLACE_START_GAP="$ICON_PLACE_START_GAP"
	NEWICON_PLACE_SPACING="$ICON_PLACE_SPACING"
fi

#disable RAM save interval and floppy icon if logical
case $PUPMODE in 7|13) SENSITIVE_RAMSAVE="true";; *) SENSITIVE_RAMSAVE="false";; esac
[ -e /sys/block/fd0 ] && SENSITIVE_FD0="true" || SENSITIVE_FD0="false"

#===============================================================

if [ -z "$ROX_DESKTOP" ] ; then
	rox_icons_lbl=""
	rox_icons=""
	auto_launch_chkbox='
            <checkbox>
              <label>'$(gettext "Auto-launch 'handler' when media plugged in")'</label>
              <default>'${HOTPLUGNOISY}'</default>
              <variable>NEWHOTPLUGNOISY</variable>
            </checkbox>'
else
	[ "$SHOWMODE" = "desktop" ] && PAGE_NR=3 || PAGE_NR=0
	auto_launch_chkbox=''
	rox_icons_lbl='|'$(gettext 'ROX Icons')
	rox_icons='    <frame '$(gettext 'Drive icons on desktop')'>
        <vbox space-expand="true" space-fill="false">
          '"`/usr/lib/gtkdialog/xml_info scale cd_mount.svg 60 "$(gettext "Normally you have to click on a drive icon to run the 'handler' but this checkbox makes the 'handler' run as soon as a media is plugged in. It shows a dialog.")"`"'
          <vbox space-expand="false" space-fill="false">
            <checkbox>
              <label>'$(gettext "Auto-launch 'handler' when media plugged in")'</label>
              <default>'${HOTPLUGNOISY}'</default>
              <variable>NEWHOTPLUGNOISY</variable>
            </checkbox>
          </vbox>
        </vbox>

       <vbox space-expand="true" space-fill="false">
          '"`/usr/lib/gtkdialog/xml_info scale floppy.svg 60 "$(gettext 'Linux does not support hotplug detection when a floppy disk is inserted or removed. If /sys/block/fd0 exists, tick this box to have a permanent icon on desktop (regardless whether a diskette is actually inserted, or even a floppy drive!).')"`"'
          <vbox space-expand="false" space-fill="false">
            <checkbox sensitive="'${SENSITIVE_FD0}'">
              <label>'$(gettext 'Add floppy drive icon on ROX desktop')'</label>
              <default>'${FD0ICON}'</default>
              <variable>NEWFD0ICON</variable>
            </checkbox>
          </vbox>
        </vbox>

        <vbox space-expand="false" space-fill="true">
          <vbox space-expand="false" space-fill="false">
            <hbox>
              <vbox homogeneous="true">
                <checkbox>
                  <label>'$(gettext 'Show desktop icons for each DRIVE')'</label>
                  <default>'${ICONDESK}'</default>
                  <variable>NEWICONDESK</variable>
                  <action>if true enable:NEWICONPARTITIONS</action>
                  <action>if true enable:NEWICON_PLACE_ORIENTATION</action>
                  <action>if true enable:NEWICON_PLACE_EDGE_GAP</action>
                  <action>if true enable:NEWICON_PLACE_START_GAP</action>
                  <action>if true enable:NEWICON_PLACE_SPACING</action>
                  <action>if false disable:NEWICONPARTITIONS</action>
                  <action>if false disable:NEWICON_PLACE_ORIENTATION</action>
                  <action>if false disable:NEWICON_PLACE_EDGE_GAP</action>
                  <action>if false disable:NEWICON_PLACE_START_GAP</action>
                  <action>if false disable:NEWICON_PLACE_SPACING</action>
                  <action condition="sensitive_is_true(NEWICONPARTITIONS)">enable:NEWLABELPARTITIONS</action>
			      <action condition="sensitive_is_false(NEWICONPARTITIONS)">disable:NEWLABELPARTITIONS</action>
                  <action condition="active_is_false(NEWICONPARTITIONS)">disable:NEWLABELPARTITIONS</action>
                </checkbox>
                <checkbox>
                  <label>'$(gettext 'Show desktop icons for each PARTITION')'</label>
                  <default>'${ICONPARTITIONS}'</default>
                  <variable>NEWICONPARTITIONS</variable>
                  <action>if true enable:NEWLABELPARTITIONS</action>
                  <action>if false disable:NEWLABELPARTITIONS</action>
                </checkbox>
                
               <checkbox>
                 <label>'$(gettext 'Show labels for each PARTITION')'</label>
                 <default>'${LABELPARTITIONS}'</default>
                 <variable>NEWLABELPARTITIONS</variable>
               </checkbox>
               <checkbox>
                  <label>'$(gettext 'Refresh / Realign existing icons')'</label>
                  <default>false</default>
                  <variable>NEWICONWIPE</variable>
               </checkbox>

              </vbox>
              <text space-fill="true" space-expand="true"><label>""</label></text>
              <vbox>
                <hbox>
                  <text xalign="0"><label>'$(gettext 'Placement: ')'</label></text>
                  <text space-fill="true" space-expand="true"><label>""</label></text>
                  <comboboxtext>
                    <default>'${ICON_PLACE_ORIENTATION}'</default>
                    <variable>NEWICON_PLACE_ORIENTATION</variable>
                    <item>top</item>
                    <item>bottom</item>
                    <item>left</item>
                    <item>right</item>
                  </comboboxtext>
                </hbox>
                <hbox>
                  <text xalign="0"><label>'$(gettext 'Start gap: ')'</label></text>
                  <text space-fill="true" space-expand="true"><label>""</label></text>
                  <spinbutton range-min="32" range-max="1024" range-step="1" range-value="'${ICON_PLACE_START_GAP}'">
                    <variable>NEWICON_PLACE_START_GAP</variable>
                  </spinbutton>
                </hbox>
                <hbox>
                  <text xalign="0"><label>'$(gettext 'Edge gap: ')'</label></text>
                  <text space-fill="true" space-expand="true"><label>""</label></text>
                  <spinbutton range-min="32" range-max="1024" range-step="1" range-value="'${ICON_PLACE_EDGE_GAP}'">
                    <variable>NEWICON_PLACE_EDGE_GAP</variable>
                  </spinbutton>
                </hbox>
                <hbox>
                  <text xalign="0"><label>'$(gettext 'Spacing: ')'</label></text>
                  <text space-fill="true" space-expand="true"><label>""</label></text>
                  <spinbutton range-min="48" range-max="256" range-step="1" range-value="'${ICON_PLACE_SPACING}'">
                    <variable>NEWICON_PLACE_SPACING</variable>
                  </spinbutton>
                </hbox>
              </vbox>     
            </hbox>
          </vbox>
        </vbox>
 
    </frame>'
    rox_signals='<action signal="show" condition="command_is_true([[ '${BACKENDON}' != true ]] && echo true)">disable:NEWAUTOUNMOUNT</action>
<action signal="show" condition="command_is_true([[ '${ICONDESK}' != true ]] && echo true)">disable:NEWICONPARTITIONS</action>
<action signal="show" condition="command_is_true([[ '${ICONDESK}' != true ]] && echo true)">disable:NEWLABELPARTITIONS</action>
<action signal="show" condition="command_is_true([[ '${ICONPARTITIONS}' != true ]] && echo true)">disable:NEWLABELPARTITIONS</action>
<action signal="show" condition="command_is_true([[ '${ICONDESK}' != true ]] && echo true)">disable:NEWICON_PLACE_ORIENTATION</action>
<action signal="show" condition="command_is_true([[ '${ICONDESK}' != true ]] && echo true)">disable:NEWICON_PLACE_EDGE_GAP</action>
<action signal="show" condition="command_is_true([[ '${ICONDESK}' != true ]] && echo true)">disable:NEWICON_PLACE_START_GAP</action>
<action signal="show" condition="command_is_true([[ '${ICONDESK}' != true ]] && echo true)">disable:NEWICON_PLACE_SPACING</action>
<action signal="show" condition="command_is_true([ '${RAMSAVEINTERVAL}' -ne 0 ] && echo true )">hide:VARASK</action>'
fi

#===============================================================

export Puppy_Event_Manager='
<window title="'$(gettext 'Puppy Event Manager')'" icon-name="gtk-execute">
<vbox space-expand="true" space-fill="true">
  <notebook tab-pos="2" page="'$PAGE_NR'" labels="'$(gettext 'Activate')'|'$(gettext 'Save Session')'|'$(gettext 'Power')${rox_icons_lbl}'">
    <frame '$(gettext 'Activate Puppy Event Manager')'>
      '"`/usr/lib/gtkdialog/xml_info scale execute_yes.svg 60 "$(gettext "The 'pup_event' Puppy Event Manager runs in the background and handles hotplugging of drives, including the desktop drive icons. This also includes automatic module and firmware loading if new hardware is detected. However, turn it all off, and Puppy will revert to the same behaviour as versions prior to 4.x, in which there is no hotplug support. This reduces resource usage, so try unticking these on very old slow PCs. Pmount can still be run manually to mount and unmount partitions.")"`"'
      <vbox space-expand="false" space-fill="false">
        <checkbox>
          <label>'$(gettext 'Activate Puppy Event Manager')'</label>
          <default>'${BACKENDON}'</default>
          <variable>NEWBACKENDON</variable>
          <action>if true enable:NEWHOTPLUGON</action>
          <action>if true enable:NEWHOTPLUGNOISY</action>
          <action>if false disable:NEWHOTPLUGON</action>
          <action>if false disable:NEWAUTOUNMOUNT</action>
          <action>if false disable:NEWHOTPLUGNOISY</action>
        </checkbox>
        <checkbox>
          <label>'$(gettext 'Activate desktop hotplug support')'</label>
          <default>'${HOTPLUGON}'</default>
          <variable>NEWHOTPLUGON</variable>
        </checkbox>
        '${auto_launch_chkbox}'
      </vbox>
    </frame>

    <frame '$(gettext 'Periodic save from RAM')'>
      <vbox space-expand="true" space-fill="true">
        '"`/usr/lib/gtkdialog/xml_info scale puppy_save.svg 60 "$(gettext "Puppy runs in a particular 'state' that we call the PUPMODE. This depends on the type of installation and the type of hardware. Currently, PUPMODE=")${PUPMODE}. $(gettext "If it is 7 or 13, then Puppy is doing everything in RAM and saving the current session to a drive periodically -- this is done in the case of Flash memory to make it last longer. When Puppy runs PUPMODE 7 or 13, there will be a 'save' icon on the desktop, also a save occurs at shutdown. You can also specify a periodic save here")" " " "<b>$(gettext 'Note!')</b> $(gettext 'PUPMODE=77 (multisession DVD) excluded, only manual save with desktop icon and at shutdown. <b>Note:</b> PUPMODE=5 there are no saves.')"`"'
        <vbox space-expand="false" space-fill="false">
          <hbox>
            <text space-expand="false" space-fill="false"><label>"'$(gettext 'Save interval (0=never)')'  "</label></text>
            <entry max_length="4" width-request="40" sensitive="'${SENSITIVE_RAMSAVE}'" space-expand="false" space-fill="false">
              <default>'${RAMSAVEINTERVAL}'</default>
              <variable>NEWRAMSAVEINTERVAL</variable>
            </entry>
            <text><label>"'$(gettext 'minutes')' "</label></text>
          </hbox>
         
          <vbox>
            <hseparator></hseparator>
            <hbox homogeneous="true" sensitive="'${SENSITIVE_RAMSAVE}'">
            <checkbox>
              <default>'${ASKTOSAVE}'</default>
              <variable>NEWASKTOSAVE</variable>
              <label>"'$(gettext 'Ask at shutdown whether to save session or not.
(if unticked, session will be saved automatically)')'"</label>
            </checkbox>
            </hbox>
          </vbox>
        
        </vbox>
      </vbox>
    </frame>

    <frame '$(gettext 'Power')'>
      <vbox space-expand="true" space-fill="true">
        '"`/usr/lib/gtkdialog/xml_info scale shutdown_interval.svg 60 "$(gettext 'The system is able to power-off your computer after a period of mouse inactivity. This will occur if the mouse cursor has not been moved for the designated interval.')"`"'
        <vbox space-expand="false" space-fill="false">
          <hbox>
            <text space-expand="false" space-fill="false"><label>"'$(gettext 'Inactivity interval (0=never)')'  "</label></text>
            <entry max_length="4" width-request="40" space-expand="false" space-fill="false">
              <default>'${POWERTIMEOUT}'</default>
              <variable>NEWPOWERTIMEOUT</variable>
            </entry>
            <text><label>"'$(gettext 'minutes')' "</label></text>
          </hbox>
        </vbox>
      </vbox>
    </frame>
    
    '${rox_icons}'
    
  </notebook>

  <hbox space-expand="false" space-fill="false">
    <text space-expand="true" space-fill="true"><label>""</label></text>
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Cancel")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
      <action>exit:Cancel</action>
    </button>
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Ok")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon ok`"'
      <action>exit:OK</action>
    </button>
  </hbox>
</vbox>
<action signal="show" condition="command_is_true([[ '${BACKENDON}' != true ]] && echo true)">disable:NEWHOTPLUGON</action>
'${rox_signals}'
</window>'

. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
RETSTRING="`gtkdialog -p Puppy_Event_Manager`"
[ $? -ne 0 ] && exit

eval "$RETSTRING"
#echo "$RETSTRING" #TEST
case "$EXIT" in
	abort|Cancel|"") exit ;;
	fullem) exec eventmanager ;; #running with 'desktop' param, now rerun with all tabs...
esac

#make sure that RAMSAVEINTERVAL is an integer only...
[ "$NEWRAMSAVEINTERVAL" = "" ] && NEWRAMSAVEINTERVAL=30 #412
NEWRAMSAVEINTERVAL=`echo -n "$NEWRAMSAVEINTERVAL" | sed -e 's/[^0-9]//g'`
#do not allow too-frequent saves...
[ $NEWRAMSAVEINTERVAL -gt 0 -a $NEWRAMSAVEINTERVAL -lt 5 ] && NEWRAMSAVEINTERVAL=5

#make sure 'ask to save' dialog won't appear at shutdown, if save interval > 0
[ $NEWRAMSAVEINTERVAL -ne 0 ] && NEWASKTOSAVE=false

#w007 make sure that POWERTIMEOUT is integer only...
[ "$NEWPOWERTIMEOUT" = "" ] && NEWPOWERTIMEOUT=0
NEWPOWERTIMEOUT=`echo -n "$NEWPOWERTIMEOUT" | sed -e 's/[^0-9]//g'`
#do not allow too-frequent power-down...
[ $NEWPOWERTIMEOUT -gt 0 -a $NEWPOWERTIMEOUT -lt 5 ] && NEWPOWERTIMEOUT=5

#v403 if major-switch has turned off hotplug support, only show one icon...
[ "$NEWBACKENDON" = "" ] && NEWBACKENDON="true" #v412
[ "$NEWHOTPLUGON" = "" ] && NEWHOTPLUGON="true" #v412
[ "$NEWBACKENDON" = "false" ] && NEWHOTPLUGON="false"
if [ "$NEWHOTPLUGON" = "false" ];then
 NEWHOTPLUGNOISY="false"
fi

if [ ! "$ROX_DESKTOP" ] ; then
	NEWICONDESK=$NEWHOTPLUGON #ICONDESK = HOTPLUGON
fi

touch /etc/eventmanager

# $1=field $2=value
set_em_item() {
	local field=$1 value=$2
	if grep -q "^${field}=" /etc/eventmanager ; then
		sed -i -e "s/^${field}=.*/${field}=${value}/" /etc/eventmanager
	else
		echo "${field}=${value}" >> /etc/eventmanager
	fi
}

set_em_item RAMSAVEINTERVAL ${NEWRAMSAVEINTERVAL}
set_em_item ASKTOSAVE ${NEWASKTOSAVE}
set_em_item POWERTIMEOUT ${NEWPOWERTIMEOUT}
set_em_item HOTPLUGNOISY ${NEWHOTPLUGNOISY}
set_em_item HOTPLUGON ${NEWHOTPLUGON}
set_em_item BACKENDON ${NEWBACKENDON}
# ICONDESK is rox-specific but it's needed for HOTPLUGNOISY
set_em_item ICONDESK ${NEWICONDESK}

if [ "$ROX_DESKTOP" ] ; then
	if [ "$NEWHOTPLUGON" = "false" ];then
		NEWICONDESK="false"
		NEWICONPARTITIONS="false"
		NEWLABELPARTITIONS="false"
		NEWHOTPLUGNOISY="false"
		NEWAUTOTARGET="false"
		NEWAUTOUNMOUNT="false" #v424
	fi
	if [ "$NEWICONDESK" = "false" ] ; then
		NEWICONPARTITIONS="false"
	fi

	set_em_item ICONDESK ${NEWICONDESK}
	set_em_item ICONPARTITIONS ${NEWICONPARTITIONS}
	set_em_item LABELPARTITIONS ${NEWLABELPARTITIONS}
	set_em_item FD0ICON ${NEWFD0ICON}
	set_em_item ICON_PLACE_ORIENTATION ${NEWICON_PLACE_ORIENTATION}
	set_em_item ICON_PLACE_EDGE_GAP ${NEWICON_PLACE_EDGE_GAP}
	set_em_item ICON_PLACE_START_GAP ${NEWICON_PLACE_START_GAP}
	set_em_item ICON_PLACE_SPACING ${NEWICON_PLACE_SPACING}
	#--
	[ "$NEWHOTPLUGON" != "$HOTPLUGON" ] && changed+=" HOTPLUGON" && CLEAN_ROX_DESK_ICONS=yes
	[ "$NEWBACKENDON" != "$BACKENDON" ] && changed+=" BACKENDON" && CLEAN_ROX_DESK_ICONS=yes
	[ "$NEWFD0ICON" != "$FD0ICON" ] && changed+=" FD0ICON" && CLEAN_ROX_DESK_ICONS=yes
	[ "$NEWICON_PLACE_ORIENTATION" != "$ICON_PLACE_ORIENTATION" ] && changed+=" ICON_PLACE_ORIENTATION" && CLEAN_ROX_DESK_ICONS=yes
	[ "$NEWICON_PLACE_EDGE_GAP" != "$ICON_PLACE_EDGE_GAP" ] && changed+=" ICON_PLACE_EDGE_GAP" && CLEAN_ROX_DESK_ICONS=yes
	[ "$NEWICON_PLACE_START_GAP" != "$ICON_PLACE_START_GAP" ] && changed+=" ICON_PLACE_START_GAP" && CLEAN_ROX_DESK_ICONS=yes
	[ "$NEWICON_PLACE_SPACING" != "$ICON_PLACE_SPACING" ] && changed+=" ICON_PLACE_SPACING" && CLEAN_ROX_DESK_ICONS=yes
	[ "$NEWICONDESK" != "$ICONDESK" ] && changed+=" ICONDESK" && CLEAN_ROX_DESK_ICONS=yes
	[ "$NEWICONPARTITIONS" != "$ICONPARTITIONS" ] && changed+=" ICONPARTITIONS" && CLEAN_ROX_DESK_ICONS=yes
	[ "$NEWLABELPARTITIONS" != "$LABELPARTITIONS" ] && changed+=" LABELPARTITIONS" && CLEAN_ROX_DESK_ICONS=yes
	[ "$NEWICONWIPE" = "true" ] && changed+=" NEWICONWIPE" && CLEAN_ROX_DESK_ICONS=yes
	#--
	if [ "$CLEAN_ROX_DESK_ICONS" = "yes" ] ; then
		echo "$changed"
		killall frontend_startup 2>/dev/null
		killall frontend_timeout 2>/dev/null
		killall pup_event_frontend_d 2>/dev/null
		export SHOW_SPLASH=1
		pup_event_frontend_d &
		touch /var/local/pup_event_icon_change_flag
		clean_desk_icons
		roxfiler -p "$HOME/Choices/ROX-Filer/PuppyPin"
	fi
	touch /var/local/pup_event_icon_change_flag

else
	# other desktops than rox
	if [ "$NEWHOTPLUGON" != "$HOTPLUGON" -o "$NEWBACKENDON" != "$BACKENDON" ] ; then
		killall frontend_startup 2>/dev/null
		killall frontend_timeout 2>/dev/null
		killall pup_event_frontend_d 2>/dev/null
		export SHOW_SPLASH=1
		pup_event_frontend_d &
	fi
fi

### END ###